package patrol.mobile.com.mobilepatrol.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.bean.HomeDataBean;

/**
 * Created by admin on 2018/9/9.
 */

public class MyHomeNewAdapter extends RecyclerView.Adapter<MyHomeNewAdapter.ViewHolder> {

    private Context mContext;
    private List<HomeDataBean> list;
//    private int[] imgTitle;

    public MyHomeNewAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<HomeDataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setTitleData(int[] imgTitle) {
//        this.imgTitle = imgTitle;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_common_home_new, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.titleText.setText(list.get(position).getTopTitle());
//        holder.titleView.setImageResource(imgTitle[position]);
        if (holder.mRecyclerView.getAdapter() == null) {
            holder.mRecyclerView.setAdapter(new GridAdapter(position));
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final RecyclerView mRecyclerView;
        public TextView titleText;
        public ImageView titleView;

        public ViewHolder(View itemView) {
            super(itemView);
            titleText = itemView.findViewById(R.id.titleText);
            titleView = itemView.findViewById(R.id.titleView);
            mRecyclerView = (RecyclerView) itemView.findViewById(R.id.contentRecyclerView);
            RecyclerView.LayoutManager manager = new GridLayoutManager(itemView.getContext(), 3);
            manager.setAutoMeasureEnabled(true);
            mRecyclerView.setLayoutManager(manager);
        }
    }

    public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> implements View.OnClickListener {
        private int parentPosition;

        public GridAdapter(int position) {
            this.parentPosition = position;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_common_sub_content, parent, false);
            view.setOnClickListener(this);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.itemView.setTag(position);
            holder.itemTet.setText(list.get(parentPosition).getData().get(position).getTitle());
            holder.itemImage.setImageResource(list.get(parentPosition).getData().get(position).getImg());
        }

        @Override
        public int getItemCount() {
            return list.get(parentPosition).getData().size();
        }

        @Override
        public void onClick(View view) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(parentPosition, (Integer) view.getTag());
            }
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView itemImage;
            public TextView itemTet;

            public ViewHolder(View itemView) {
                super(itemView);
                itemImage = itemView.findViewById(R.id.itemImage);
                itemTet = itemView.findViewById(R.id.itemTet);
            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int parentPosition, int childPosition);
    }

    private OnItemClickListener mItemClickListener;

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }
}
